package com.yx.auto.service;

import com.yx.auto.contanct.Constant;
import com.yx.auto.http.ClientHttp;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author yinxing
 * @date 2022-08-24
 */
@Service
public class HttpServiceImpl implements HttpService{

    @Resource
    private ClientHttp clientHttp;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String caseHttp(String fileName,String methodName) throws FileNotFoundException {
        Yaml yaml = new Yaml();
        FileReader fileReader = new FileReader(this.getClass().getClassLoader().getResource(fileName).getPath());
        BufferedReader buffer = new BufferedReader(fileReader);
        Map<String, Object> map = yaml.load(buffer);
        Map<String, Object> method;
        method = (Map<String, Object>) map.get(methodName);
        if (method.get(Constant.type).equals(Constant.get)) {
            Object o = clientHttp.sendGet(method.get(Constant.header).toString(), method.get(Constant.header));
            System.out.println(o.toString());
        } else {
            Map<String, Object> header = (Map<String, Object>) method.get(Constant.header);
            for (Map.Entry<String, Object> headerOne : header.entrySet()) {
                if (headerOne.getKey().contains("x-ts")) {
                    header.put("x-ts", System.currentTimeMillis());
                }
                if (headerOne.getValue().toString().contains(Constant.redis)){
                    String split = headerOne.getValue().toString().
                            substring(headerOne.getValue().toString().lastIndexOf('.')+1);
                    header.put(headerOne.getKey(),redisTemplate.opsForValue().get(split));
                }
            }
            Map<String, Object> requests = (Map<String, Object>) method.get(Constant.request);
            for (Map.Entry<String, Object> request : requests.entrySet()) {
                if (request.getValue().toString().contains(Constant.redis)){
                    String split = request.getValue().toString().
                            substring(request.getValue().toString().lastIndexOf('.')+1);
                    requests.put(request.getKey(),redisTemplate.opsForValue().get(split));
                }
            }
            Object o = clientHttp.sendPost(method.get(Constant.url).toString(), header, requests);
            System.out.println(o.toString());
        }
        return "success";
    }

    @Override
    public String login() throws FileNotFoundException {
        Yaml yaml = new Yaml();
        FileReader fileReader = new FileReader(this.getClass().getClassLoader().getResource("login.yml").getPath());
        BufferedReader buffer = new BufferedReader(fileReader);
        Map<String, Object> map = yaml.load(buffer);
        Map<String, Object> method = (Map<String, Object>) map.get("login");
        if (method.get("type").equals("Get")) {
            Object o = clientHttp.sendGet(method.get("url").toString(), method.get("header"));
            System.out.println(o.toString());
        } else {
            Map<String, Object> header = (Map<String, Object>) method.get("header");
            if (header.get("x-ts") != null) {
                header.put("x-ts", System.currentTimeMillis());
            }
            if (header.get("accessToken") != null) {
                Object accessToken = redisTemplate.opsForValue().get("accessToken");
                if (accessToken == null) {

                }
                header.put("accessToken", redisTemplate.opsForValue().get("accessToken"));
            }
            Map<String, Object> request = (Map<String, Object>) method.get("request");
            Object o = clientHttp.sendPost(method.get("url").toString(), header, request);
            System.out.println(o.toString());
            Map<String, Object> result = (Map<String, Object>) o;
            Map<String, Object> content = (Map<String, Object>) result.get("content");
            redisTemplate.opsForValue().set("accessToken", content.get("accessToken").toString(), Long.parseLong(content.get("accessTokenExpireIn").toString()), TimeUnit.SECONDS);
            redisTemplate.opsForValue().set("userId",content.get("userId").toString());
            String accessToken = redisTemplate.opsForValue().get("accessToken");
            System.out.println("登录凭证accessToken:" + accessToken);
        }
        return null;
    }

}
